/**
 * StorageUtil.java
 * 版权所有(C) 2013
 * 创建者:cuiran 2013-11-14 下午3:21:13
 */
package com.ghyf.mplay.util;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import com.ghyf.mplay.value.ConstantValue;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

/**
 * 获取手机内部存储空间和外部存储空间
 * @author cuiran
 * @version 1.0.0
 */
public class StorageUtil {

	private static final String TAG="StorageUtil";
	 private static final int ERROR = -1;

	    /**
	     * SDCARD是否存
	     */
	    public static boolean externalMemoryAvailable() {
	        return android.os.Environment.getExternalStorageState().equals(
	                android.os.Environment.MEDIA_MOUNTED);
	    }

	    /**
	     * 获取手机内部剩余存储空间
	     * @return
	     */
	    public static long getAvailableInternalMemorySize() {
	        File path = Environment.getDataDirectory();
	        StatFs stat = new StatFs(path.getPath());
	        long blockSize = stat.getBlockSize();
	        long availableBlocks = stat.getAvailableBlocks();
	        return availableBlocks * blockSize;
	    }

	    /**
	     * 获取手机内部总的存储空间
	     * @return
	     */
	    public static long getTotalInternalMemorySize() {
	        File path = Environment.getDataDirectory();
	        StatFs stat = new StatFs(path.getPath());
	        long blockSize = stat.getBlockSize();
	        long totalBlocks = stat.getBlockCount();
	        return totalBlocks * blockSize;
	    }

	    /**
	     * 获取SDCARD剩余存储空间
	     * @return
	     */
	    public static long getAvailableExternalMemorySize() {
	        if (externalMemoryAvailable()) {
	            File path = Environment.getExternalStorageDirectory();
	            StatFs stat = new StatFs(path.getPath());
	            long blockSize = stat.getBlockSize();
	            long availableBlocks = stat.getAvailableBlocks();
	            return availableBlocks * blockSize/1024/1024;
	        } else {
	            return ERROR;
	        }
	    }

	    /**
	     * 获取SDCARD总的存储空间
	     * @return
	     */
	    public static long getTotalExternalMemorySize() {
	        if (externalMemoryAvailable()) {
	            File path = Environment.getExternalStorageDirectory();
	            StatFs stat = new StatFs(path.getPath());
	            long blockSize = stat.getBlockSize();
	            long totalBlocks = stat.getBlockCount();
	            return totalBlocks * blockSize/1024/1024;
	        } else {
	            return ERROR;
	        }
	    }
	    /**
	     * 数据硬盘总空间<br>
	     * 2014-2-25 上午11:10:57
	     * @return
	     *
	     */
	    public static long getDataTotalSize(){
	    	 StatFs stat = new StatFs("/mnt/sata");
		     long blockSize = stat.getBlockSize();
		     long totalBlocks = stat.getBlockCount()*blockSize/1024/1024;
//		     long availableBlocks =stat.getFreeBlocks()*blockSize/1024/1024;
		     return totalBlocks;
	    }
	    /**
	     * 数据硬盘剩余空间<br>
	     * 2014-2-25 上午11:10:57
	     * @return
	     *
	     */
	    public static long getDataFreeSize(){
	    	 StatFs stat = new StatFs("/mnt/sata");
		     long blockSize = stat.getBlockSize();
		     long availableBlocks =stat.getFreeBlocks()*blockSize/1024/1024;
		     return availableBlocks;
	    }
	    /**
	     * 数据硬盘<br>
	     * 2014-2-25 上午11:10:57
	     * @return
	     *
	     */
	    public static int getDataUse(){
	    	int number=0;
	    	 StatFs stat = new StatFs("/mnt/sata");
		     long blockSize = stat.getBlockSize();
		     long totalBlocks = stat.getBlockCount()*blockSize/1024/1024;
		     long availableBlocks =stat.getFreeBlocks()*blockSize/1024/1024;
//		     LogUtil.i(TAG, "installApp  totalBlocks "+totalBlocks+",blockSize "+blockSize+" availableBlocks "+availableBlocks);
		     number= (int)(100*(totalBlocks-availableBlocks)/totalBlocks) ;
		       
	    	
	    	return number;
	    }
	    /**
	     * 系统硬盘<br>
	     * 2014-2-25 上午11:10:57
	     * @return
	     *
	     */
	    public static int getHardUse(){
	    	int number=0;
	    	 StatFs stat = new StatFs(ConstantValue.SHOT_SDCARD_DIR);
		     long blockSize = stat.getBlockSize();
		     long totalBlocks = stat.getBlockCount()*blockSize/1024/1024;
		     
		     long availableBlocks =stat.getFreeBlocks()*blockSize/1024/1024;
//		     LogUtil.i(TAG, "getDataUse  totalBlocks "+totalBlocks+",blockSize "+blockSize+" availableBlocks "+availableBlocks);
		     number= (int)(100*(totalBlocks-availableBlocks)/totalBlocks) ;
		       
	    	
	    	return number;
	    }
	    /**
	     * 获取CPU使用率
	     * 2014-2-24 下午1:28:48
	     * @param context
	     * @return
	     *
	     */
	    @SuppressLint("NewApi")
		public static int getRamUse(Context context){
	    	int number=0;
	    	try{
	    		
	    		ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
	    		//获得MemoryInfo对象
	            MemoryInfo memoryInfo = new MemoryInfo() ;
	            //获得系统可用内存，保存在MemoryInfo对象上
	            mActivityManager.getMemoryInfo(memoryInfo) ;
	            //可用内存
	            long memSize = memoryInfo.availMem;
	           
	            //总内存
	            long totalSize=814*1024*1024;
	            
	            float num=(totalSize-memSize)*100/totalSize;
	            number=(int)num;
	            
	    	}catch(Exception e){
	    		 LogUtil.e(TAG, "getCpuUse error", e);
	    	}
	    	
	    	return number;
	    }
	    
	   /**
	    * 
	    * 2014-7-14 下午7:36:57
	    * @param context
	    * @return
	    *
	    */
	    public static int getRamUse1(Context context){
	    	int number=0;
	    	try{
	    		
	    		ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
	    		//获得MemoryInfo对象
	            MemoryInfo memoryInfo = new MemoryInfo() ;
	            //获得系统可用内存，保存在MemoryInfo对象上
	            mActivityManager.getMemoryInfo(memoryInfo) ;
	            //可用内存
	            long memSize = memoryInfo.availMem>>10;
	            boolean lowMemory=memoryInfo.lowMemory;
	            //
	            long threshold = memoryInfo.threshold;
	            LogUtil.e(TAG, "getCpuUse  memSize="+memSize+",lowMemory="+lowMemory+",threshold="+threshold);
	            
	    	}catch(Exception e){
	    		 LogUtil.e(TAG, "getCpuUse error", e);
	    	}
	    	
	    	return number;
	    }
	    
	    public static int readUsage() {

	    	try {
	    		RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
	    		String load = reader.readLine();
	    		String[] toks = load.split(" ");
//	    		LogUtil.i(TAG, "installApp  ram"+load);
	    		long idle1 = Long.parseLong(toks[5]);
//	    		 LogUtil.i(TAG, "installApp  idle1"+idle1);
	    		long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3])
	    				+ Long.parseLong(toks[4]) + Long.parseLong(toks[6])
	    				+ Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
	    	    
	    		try {
	    			Thread.sleep(1000);

	    		} catch (Exception e) {
	    		}
	    		reader.seek(0);
	    		load = reader.readLine();
	    		reader.close();
	    		toks = load.split(" ");
	    		long idle2 = Long.parseLong(toks[5]);
	    		
	    		long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3])
	    				+ Long.parseLong(toks[4]) + Long.parseLong(toks[6])
	    				+ Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

	    		return (int) (100 * (cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1)));

	    	} catch (IOException ex) {
	    		ex.printStackTrace();

	    	}
	    	return 0;

	    }
	    
	   
}
